#!/bin/bash

# Slackware build script for fmit (Free Musical Instrument Tuner)

# Copyright 2013 Josiah Boothby, Seattle
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# 20220218 bkw: Modified by SlackBuilds.org:
# - update for v1.2.14 (the old download disappeared).
# - autodetect jack and portaudio, instead of making the user manually
#   set environment variables (can still disable with environment).

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=fmit
VERSION=${VERSION:-1.2.14}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

# 20220218 bkw: always include qt (aka PulseAudio) and ALSA support.
CONFIG="acs_qt acs_alsa"

# 20220218 bkw: autodetect jack and portaudio, allow disabling in env.
pkg-config --exists jack && \
  [ "${JACK:-yes}" != "no" ] && \
  CONFIG+=" acs_jack" && \
  SETCAP="${SETCAP:-yes}"
pkg-config --exists portaudio-2.0 && \
  [ "${PORTAUDIO:-yes}" != "no" ] && \
  CONFIG+=" acs_portaudio"

# 20220218 bkw: build without OSS, allow enabling in env.
[ "${OSS:-no}" = "yes" ] && CONFIG+=" acs_oss"

echo "=== CONFIG='$CONFIG'"

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# 20220218 bkw: SLKCFLAGS and LIBDIRSUFFIX not needed (qmake uses
# the ones qt5 was compiled with, which is correct).

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tar.gz
cd $PRGNAM-$VERSION
chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \+ -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \+

qmake-qt5 PREFIX=/usr CONFIG+="$CONFIG"
make
make install INSTALL_ROOT=$PKG

# 20220218 bkw: binary already stripped.

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a COPYING* README.txt $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
sed "s,@CONFIG@,$CONFIG," < $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

if [ "$SETCAP" = "yes" ]; then
  cat $CWD/setcap.sh >> $PKG/install/doinst.sh
  chown root:audio $PKG/usr/bin/$PRGNAM
  chmod 0750 $PKG/usr/bin/$PRGNAM
fi

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
